#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Table S.4
#####

library(here)
library(data.table)
library(texreg)

# source the custom functions
source("functions.R")

# load the pooled data if not already in workspace
if(!exists("pooled")){
  pooled <- fread(here("data", "pooled.csv"))
}

# define the imbalanced covaraites
imbMain <- c("age", "ageSq")


# Fitting -----------------------------------------------------------------

s4_1 <- noCov(mechanism_NM, chatty = F)
s4_2 <- allCov(mechanism_NM, chatty = F)
s4_3 <- flexCov(mechanism_NM, covs = imbMain, chatty = F)

s4_4 <- noCov(mechanism_CS, chatty = F)
s4_5 <- allCov(mechanism_CS, chatty = F)
s4_6 <- flexCov(mechanism_CS, covs = imbMain, chatty = F)

s4_7 <- noCov(mechanism_CH, chatty = F)
s4_8 <- allCov(mechanism_CH, chatty = F)
s4_9 <- flexCov(mechanism_CH, covs = imbMain, chatty = F)


# Reporting ---------------------------------------------------------------

# in the document, these three outputs are merged to a single latex table.

texregPS(
  list(s4_1, s4_2, s4_3),
  custom.model.names = paste0("(", 1:3, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)

texregPS(
  list(s4_4, s4_5, s4_6),
  custom.model.names = paste0("(", 4:6, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)

texregPS(
  list(s4_7, s4_8, s4_9),
  custom.model.names = paste0("(", 7:9, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)
